package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.BoJDealFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.DefeatOrderFragment;
import com.bjesc_b2c.app.appraiset.widget.popupwindow.TrackPop;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;

import butterknife.Bind;
import butterknife.OnClick;
import rxbus_base.RxBus;

/**
 * Created by yidong on 2017/8/8.
 */
public class CompleteOrderActivity  extends BaseActivity {
    @Bind(R.id.viewpager)
    ViewPager mViewPager;
    @Bind(R.id.viewpager_tab)
    SmartTabLayout mViewpagerTab;
    @Bind(R.id.title_view)
    View titleView;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.iv_return)
    LinearLayout returnLin;
    @Bind(R.id.tv_car_name_title)
    TextView nameTv;
    @Bind(R.id.edit_search_name)
    EditText searchEdit;
    private TrackPop trackPop;
    private Intent intent = new Intent();
    private int type = 1;
    @Override
    protected int getLayout() {
        return R.layout.activity_await_task;
    }

    @Override
    protected void initEventAndData() {
        titleTv.setText("已完成订单");
        returnLin.setVisibility(View.VISIBLE);
        final LayoutInflater inflater = LayoutInflater.from(this);
        final int[] tabTitles = {R.string.bojing_deal, R.string.defeat};
        FragmentPagerItems pages = FragmentPagerItems.with(this)
                .add(R.string.bojing_deal, BoJDealFragment.class)
                .add(R.string.defeat, DefeatOrderFragment.class)
                .create();
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getSupportFragmentManager(), pages);
        mViewPager.setOffscreenPageLimit(pages.size());
        mViewPager.setAdapter(adapter);
        mViewpagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                View view = inflater.inflate(R.layout.layout_navigation_top_item, container, false);
                TextView titleView = (TextView) view.findViewById(R.id.txt_title);
                TextView titleNum = (TextView) view.findViewById(R.id.txt_number);
                titleNum.setVisibility(View.GONE);
                titleView.setText(tabTitles[position % tabTitles.length]);
                return view;
            }
        });
        mViewpagerTab.setViewPager(mViewPager);
        searchEdit.addTextChangedListener(mTextWatcher);
        getIntentFuzzyQueryA();
    }

    @Override
    protected void createPresenter() {

    }

    @OnClick({R.id.iv_return, R.id.tv_car_name_title})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.tv_car_name_title:
                trackPop = new TrackPop(this, popListener);
                trackPop.showAsDropDown(titleView, 40, 20);
                break;
        }
    }
    private View.OnClickListener popListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            switch (view.getId()) {
                case R.id.tv_client_name:
                    nameTv.setText("客户名");
                    searchEdit.setHint("请输入客户名称");
                    type = 1;
                    searchEdit.setText("");
                    break;
                case R.id.tv_car_name:
                    nameTv.setText("主车");
                    searchEdit.setHint("请输入品牌/车系");
                    searchEdit.setText("");
                    type = 2;
                    break;
                case R.id.tv_order_id:
                    searchEdit.setText("");
                    nameTv.setText("订单号");
                    type = 3;
                    searchEdit.setHint("请输入订单号");
                    break;

            }
            if (trackPop.isShowing()) {
                trackPop.dismiss();
            }
        }
    };
    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            RxBus.getInstance().sendData("keyWord", searchEdit.getText().toString());
        }
    };

    //把 确认键变成搜索键 跳过去
    public void getIntentFuzzyQueryA() {
//搜索框。EditView 新建一个事件。
        searchEdit.setOnKeyListener(new View.OnKeyListener() {//输入完后按键盘上的搜索建
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //判断是否按下。
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    //判断是否是回车键
                    if (keyCode == event.KEYCODE_ENTER) {
                        String keyWord = searchEdit.getText().toString();
                        //为NULL 判断
                        if (!TextUtils.isEmpty(keyWord)) {
                            intent.putExtra("type", type);
                            intent.putExtra("keyWord", keyWord);
                            RxBus.getInstance().sendData(CompleteOrderActivity.class.getName(), intent);
                        }
                    }
                }
                return false;
            }

        });
    }
}